package com.csu.common.doc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.util.CollectionUtils;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

/**
 * @项目名称：cloud_market
 * @类名称：SwaggerConfig
 * @类描述：
 * @创建人：TangC
 * @创建时间：2015-7-1 下午7:56:50
 */
// @Configuration
@EnableSwagger
@ComponentScan("com.csu.**.controller")
public class SwaggerConfig
{
    private SpringSwaggerConfig springSwaggerConfig;

    /*
     * 需要生成文档的部分（路径）
     */
    private List<String> includePatterns = null;
    /*
     * api标题
     */
    private String apiTitle = "";
    /*
     * api描述
     */
    private String apiDescription = "";

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    @Bean
    // Don't forget the @Bean annotation
    public SwaggerSpringMvcPlugin customImplementation()
    {
        SwaggerSpringMvcPlugin swaggerSpringMvcPlugin = new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
                .apiInfo(apiInfo());

        if (!CollectionUtils.isEmpty(includePatterns))
        {
            String[] patterns = new String[includePatterns.size()];

            for (int i = 0; i < includePatterns.size(); i++)
            {
                if (includePatterns.get(i) != null)
                {
                    patterns[i] = includePatterns.get(i);
                }
            }

            swaggerSpringMvcPlugin.includePatterns(patterns);
        }
        @SuppressWarnings("unchecked")
        SwaggerSpringMvcPlugin excludeAnnotations = swaggerSpringMvcPlugin.excludeAnnotations(ApiExclude.class);
        return excludeAnnotations;
    }

    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(this.apiTitle, // My Apps API Title
                this.apiDescription, // My Apps API Description
                "", // My Apps API terms of service
                "", // My Apps API Contact Email
                "", // My Apps API Licence Type
                "" // My Apps API License URL
        );
        return apiInfo;
    }

    /**
     * @return the apiTitle
     */
    public String getApiTitle()
    {
        return apiTitle;
    }

    /**
     * @param apiTitle
     *            the apiTitle to set
     */
    public void setApiTitle(String apiTitle)
    {
        this.apiTitle = apiTitle;
    }

    /**
     * @return the apiDescription
     */
    public String getApiDescription()
    {
        return apiDescription;
    }

    /**
     * @param apiDescription
     *            the apiDescription to set
     */
    public void setApiDescription(String apiDescription)
    {
        this.apiDescription = apiDescription;
    }

    /**
     * @return the includePatterns
     */
    public List<String> getIncludePatterns()
    {
        return includePatterns;
    }

    /**
     * @param includePatterns
     *            the includePatterns to set
     */
    public void setIncludePatterns(List<String> includePatterns)
    {
        this.includePatterns = includePatterns;
    }

}
